<template>
	<div class="container add_edit">
		<el-form v-loading="formLoading" :model="form" :rules="rules" ref="ruleForm" label-width="100px">
			<el-form-item label="地址">
				<el-input :disabled="addBtnLoading" v-model="form.adress" placeholder="请输入地址" clearable>
				</el-input>
			</el-form-item>
			<el-form-item label="号码">
				<el-input :disabled="addBtnLoading" v-model="form.mobile" placeholder="请输入号码" clearable>
				</el-input>
			</el-form-item>
			<el-form-item label="邮箱">
				<el-input :disabled="addBtnLoading" v-model="form.email" placeholder="请输入邮箱" clearable>
				</el-input>
			</el-form-item>
			<el-form-item label="微信号">
				<el-input :disabled="addBtnLoading" v-model="form.wechat" placeholder="请输入微信号" clearable>
				</el-input>
			</el-form-item>
			<!-- <el-form-item label="二维码">
				<uni-file-picker :disabled="addBtnLoading" :disable-preview="addBtnLoading" :del-icon="!addBtnLoading"
					v-model="qrcodeValue" fileMediatype="image" returnType="object" :image-styles="imageStyle"
					@success="imgUpload" @delete="imgDelete" />
			</el-form-item>
			<el-form-item label="顶部图片">
				<uni-file-picker :disabled="addBtnLoading" :disable-preview="addBtnLoading" :del-icon="!addBtnLoading"
					v-model="topImageValue" fileMediatype="image" returnType="object" :image-styles="imageStyle"
					@success="imgTopUpload" @delete="imgTopDelete" />
			</el-form-item> -->
			<el-form-item>
				<el-button round icon="el-icon-check" :loading="addBtnLoading" class="confirm-btn"
					@click="submitForm('ruleForm')">提交</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>

<script>
	const __name = 'wmzmall-app-contact';
	export default {
		data() {
			return {
				dataId: '',
				formLoading: false,
				form: {
					adress: '',
					mobile: '',
					email: '',
					wechat: '',
					qrcode: '',
					top_image: '',
					state: '启用'
				},
				qrcodeValue: null,
				topImageValue: null,
				imageStyle: {
					height: '150px',
					width: '150px'
				},
				rules: {},
				addBtnLoading: false
			};
		},
		methods: {
			async loadData() {
				this.formLoading = true;
				await this.$db[__name].tofirst({
					loading: false
				}).then(res => {
					if (res.code == 200) {
						if (res.datas) {
							for (let item in this.form) {
								this.form[item] = res.datas[item];
							}
							this.dataId = res.datas._id;

							if (this.form.qrcode) {
								this.qrcodeValue = {};
								this.qrcodeValue.url = this.form.qrcode;
							}
							if (this.form.top_image) {
								this.topImageValue = {};
								this.topImageValue.url = this.form.top_image;
							}
						}
					}
					this.formLoading = false;
				});
			},
			submitForm(formName) {
				this.$refs[formName].validate((valid, obj) => {
					// 默认获取第一个未验证 form 属性名
					this.$api.set_unvalidated_form_focus(this, obj);
					if (valid) {
						this.addBtnLoading = true;
						if (!this.dataId) {
							this.$db[__name].add(this.form).then(res => {
								if (res.code == 200) {
									this.$message({
										message: '操作成功',
										type: 'success'
									});
									this.addBtnLoading = false;
								}
								this.loadData();
							}).catch(() => {
								this.addBtnLoading = false;
							})
						} else {
							this.$db[__name].update(this.dataId, this.form).then(res => {
								if (res.code == 200) {
									this.$message({
										message: '操作成功',
										type: 'success'
									});
									this.addBtnLoading = false;
									return;
								}
								this.loadData();
							}).catch(() => {
								this.addBtnLoading = false;
							})
						}
					}
				});
			},
			imgUpload(e) {
				this.form.qrcode = e.tempFilePaths[0];
			},
			imgDelete() {
				this.qrcodeValue = null;
				this.form.qrcode = '';
			},
			imgTopUpload(e) {
				this.form.top_image = e.tempFilePaths[0];
			},
			imgTopDelete() {
				this.topImageValue = null;
				this.form.top_image = '';
			}
		},
		created() {
			this.loadData();
		}
	};
</script>

<style></style>